//
//  STStoryApi.swift
//  Story
//
//  Created by 汤泽川 on 2019/4/18.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import Foundation
class STStoryApi {
    static public func fetchStoryList(channelId : Int, page : Int, count : Int, result : @escaping (_ response : ResponseModel<[STStoryInfo]>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<[STStoryInfo]> >()
        request.requestPath = "/story/query"
        request.requestParam = ["channelId" : channelId, "page" : page, "count" : count]
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.status != 0 {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                } else {
                    result(data, nil)
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
    static public func fetchStoryCategory(result : @escaping (_ response : ResponseModel<[STStoryCategoryModel]>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<[STStoryCategoryModel]> >()
        request.requestPath = "/story/category"
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.status != 0 {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                } else {
                    result(data, nil)
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
    static public func searchStoryList(keyword : String, page : Int, count : Int, result : @escaping (_ response : ResponseModel<[STStoryInfo]>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<[STStoryInfo]> >()
        request.requestPath = "/story/search"
        request.requestParam = ["keyword" : keyword, "page" : page, "count" : count]
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.status != 0 {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                } else {
                    result(data, nil)
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
    static public func searchAuthorStoryList(authorName : String, page : Int, count : Int, result : @escaping (_ response : ResponseModel<[STStoryInfo]>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<[STStoryInfo]> >()
        request.requestPath = "/story/search"
        request.requestParam = ["keyword" : authorName, "page" : page, "count" : count]
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.status != 0 {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                } else {
                    result(data, nil)
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
    
    static public func publishStory(storyName : String, introduce : String, channelId : Int, storyUrl : String, avatarUrl : String?, result : @escaping (_ response : ResponseModel<String>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<String> >()
        request.requestPath = "/story/upload"
        if avatarUrl != nil {
            request.requestParam = ["storyName" : storyName, "introduce" : introduce, "channelId" : channelId, "storyUrl" : storyUrl, "avatarUrl" : avatarUrl!]
        } else {
            request.requestParam = ["storyName" : storyName, "introduce" : introduce, "channelId" : channelId, "storyUrl" : storyUrl]
        }
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.status != 0 {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                } else {
                    result(data, nil)
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
}
